package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.system.domain.DrllInfo;
import com.ruoyi.system.domain.RegGroup;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.AlarmtypeMapper;
import com.ruoyi.system.domain.Alarmtype;
import com.ruoyi.system.service.IAlarmtypeService;

/**
 * 报警类型Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-02-26
 */
@Service
public class AlarmtypeServiceImpl implements IAlarmtypeService 
{
    private static final Logger log = LoggerFactory.getLogger(AlarmtypeServiceImpl.class);

    @Autowired
    private AlarmtypeMapper alarmtypeMapper;

    /**
     * 查询报警类型
     * 
     * @param id 报警类型主键
     * @return 报警类型
     */
    @Override
    public Alarmtype selectAlarmtypeById(Long id,String dbName)
    {
        return alarmtypeMapper.selectAlarmtypeById(id,dbName);
    }

    /**
     * 查询报警类型列表
     * 
     * @param alarmtype 报警类型
     * @return 报警类型
     */
    @Override
    public List<Alarmtype> selectAlarmtypeList(Alarmtype alarmtype,String dbName)
    {
        return alarmtypeMapper.selectAlarmtypeList(alarmtype,dbName);
    }

    @Override
    public List<Alarmtype> selectAlarmtypeForAll(String dbName) {
        return alarmtypeMapper.selectAlarmtypeForAll(dbName);
    }

    /**
     * 新增报警类型
     * 
     * @param alarmtype 报警类型
     * @return 结果
     */
    @Override
    public int insertAlarmtype(Alarmtype alarmtype,String dbName)
    {
        return alarmtypeMapper.insertAlarmtype(alarmtype,dbName);
    }

    /**
     * 修改报警类型
     * 
     * @param alarmtype 报警类型
     * @return 结果
     */
    @Override
    public int updateAlarmtype(Alarmtype alarmtype,String dbName)
    {
        return alarmtypeMapper.updateAlarmtype(alarmtype,dbName);
    }

    /**
     * 批量删除报警类型
     * 
     * @param ids 需要删除的报警类型主键
     * @return 结果
     */
    @Override
    public int deleteAlarmtypeByIds(Long[] ids,String dbName)
    {
        return alarmtypeMapper.deleteAlarmtypeByIds(ids,dbName);
    }

    /**
     * 删除报警类型信息
     * 
     * @param id 报警类型主键
     * @return 结果
     */
    @Override
    public int deleteAlarmtypeById(Long id,String dbName)
    {
        return alarmtypeMapper.deleteAlarmtypeById(id,dbName);
    }

    @Override
    public String importAlarmType(List<Alarmtype> alarmTypeList, Boolean isUpdateSupport, String operaName, String dbName){
        if (alarmTypeList == null || alarmTypeList.size() == 0)
        {
            throw new ServiceException("导入项目报警等级数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (Alarmtype alarmType : alarmTypeList)
        {
            try{
                // 验证是否存在
                String alarmTypeExplain = alarmType.getAlarmtypeExpain();
                Alarmtype tempAlarmType = alarmtypeMapper.selectAlarmTypeByAlarmTypeExplain(alarmTypeExplain,dbName);
                if (tempAlarmType == null)
                {
                    alarmType.setCreateBy(operaName);
                    alarmtypeMapper.insertAlarmtype(alarmType,dbName);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、报警类型说明 " + alarmType.getAlarmtypeExpain() + " 导入成功");
                }
                else if (isUpdateSupport)
                {
                    alarmType.setId(tempAlarmType.getId());
                    alarmType.setUpdateBy(operaName);
                    alarmtypeMapper.updateAlarmtype(alarmType,dbName);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、报警类型说明 " + alarmType.getAlarmtypeExpain() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、报警类型说明 " + alarmType.getAlarmtypeExpain() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、报警类型说明 " + alarmType.getAlarmtypeExpain() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }

        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
